package com.mlz.mlzmall.ui.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.MyOrderList;
import com.mlz.mlzmall.ui.activity.act.order.LogitsticsCheck;
import com.mlz.mlzmall.ui.activity.act.order.OrderPayment;
import com.mlz.mlzmall.ui.activity.act.order.ReviewActivity;
import com.mlz.mlzmall.ui.activity.act.personal.MyOrder;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.sql.Date;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;

/**
 * describe :
 * Created by FUYING on 2016/5/30.
 * PackageName com.mlz.mlzmall.ui.adapter.
 * ProjectName mlzmall_android.
 */
public class MyAdapterAll extends BaseAdapter {
    private ArrayList<MyOrderList.Data.LIST> data;
    private ArrayList<MyOrderList.Data.LIST.Product> products;
    private MyOrder context;
    private int resource;
    ImageLoader imageLoader = ImageLoader.getInstance();
    DecimalFormat df = new DecimalFormat("0.00 ");//使价格后保留两位小数


    public MyAdapterAll(MyOrder context, int resource, ArrayList<MyOrderList.Data.LIST> data, ArrayList<MyOrderList.Data.LIST.Product> products) {
        this.context = context;
        this.resource = resource;
        this.data = data;
        this.products = products;
    }

    @Override
    public int getCount() {
        if (null == products) {
            return 0;
        } else {
            return products.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(resource, null);
            mViewHolder = new ViewHolder();
            // 订单号
            mViewHolder.tvOrderCodeMyOrder = (TextView) convertView.findViewById(R.id.tvOrderCodeMyOrder);
            mViewHolder.fenge = (TextView) convertView.findViewById(R.id.fenge);
            // 订单号标题布局
            mViewHolder.RelativeLayoutTitleMyOrder = (RelativeLayout) convertView.findViewById(R.id.RelativeLayoutTitleMyOrder);
            //当前订单的状态
            mViewHolder.tvPayStatusMyOrder = (TextView) convertView.findViewById(R.id.tvPayStatusMyOrder);
            //商品的总价
            mViewHolder.tvTotalPriceMyOrder = (TextView) convertView.findViewById(R.id.tvTotalPriceMyOrder);
            //运费
            mViewHolder.tvFreightMyOrder = (TextView) convertView.findViewById(R.id.tvFreightMyOrder);
            // 订单关税
            mViewHolder.tvTaxMyOrder = (TextView) convertView.findViewById(R.id.tvTaxMyOrder);
            // 实际付款
            mViewHolder.tvRealPaymentMyOrder = (TextView) convertView.findViewById(R.id.tvRealPaymentMyOrder);
            //下单时间
            mViewHolder.tvDateAddedMyOrder = (TextView) convertView.findViewById(R.id.tvDateAddedMyOrder);
            //付款时限
            // mViewHolder.tvDateModified = (TextView) convertView.findViewById(R.id.tvDateModified);
            //商品的名称
            mViewHolder.tvProductNameMyOrder = (TextView) convertView.findViewById(R.id.tvProductNameMyOrder);
            // 商品的数量
            mViewHolder.tvQuantityMyOrder = (TextView) convertView.findViewById(R.id.tvQuantityMyOrder);
            //  商品的价格
            mViewHolder.tvPriceMyOrder = (TextView) convertView.findViewById(R.id.tvPriceMyOrder);
            //  商品的税率
            mViewHolder.tvTaxRateMyOrder = (TextView) convertView.findViewById(R.id.tvTaxRateMyOrder);
            // 付款提示布局
            mViewHolder.payDataRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.payDataRelativeLayout);
            // 付款详情布局
            mViewHolder.PayDetailRelativeLayoutMyOrder = (RelativeLayout) convertView.findViewById(R.id.PayDetailRelativeLayoutMyOrder);
            // 立即支付布局
            mViewHolder.immediatelyPayLinearLayoutMyOrder = (RelativeLayout) convertView.findViewById(R.id.immediatelyPayLinearLayoutMyOrder);
            // 确认收货布局
            mViewHolder.confirmReceivingLinearLayoutMyOrder = (RelativeLayout) convertView.findViewById(R.id.confirmReceivingLinearLayoutMyOrder);
            // 商品评价布局
            mViewHolder.ReviewProductLinearLayoutMyOrder = (RelativeLayout) convertView.findViewById(R.id.ReviewProductLinearLayoutMyOrder);
            //产品的图片
            mViewHolder.imProductMyOrder = (ImageView) convertView.findViewById(R.id.imProductMyOrder);
            // item 线性布局
            mViewHolder.LLinerLayoutMyOrder = (LinearLayout) convertView.findViewById(R.id.LLinerLayoutMyOrder);
            // 取消订单
            mViewHolder.btCancelMyOrder = (Button) convertView.findViewById(R.id.btCancelMyOrder);
            //立即支付
            mViewHolder.btImmediatePayMyOrder = (Button) convertView.findViewById(R.id.btImmediatePayMyOrder);
            // 查看物流
            mViewHolder.btLogistics_Confirm = (Button) convertView.findViewById(R.id.btLogistics_Confirm);
            mViewHolder.btLogistics_Review = (Button) convertView.findViewById(R.id.btLogistics_Review);
            // 评价商品
            mViewHolder.btReviewGoods = (Button) convertView.findViewById(R.id.btReviewGoods);
            //  确认收货
            mViewHolder.btReceiptConfirm = (Button) convertView.findViewById(R.id.btReceiptConfirm);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        // 订单号
        mViewHolder.tvOrderCodeMyOrder.setText(String.valueOf(products.get(position).getOrderCoderForgroup()));
        //商品的名称
        mViewHolder.tvProductNameMyOrder.setText(products.get(position).getName());
        // 商品的数量
        mViewHolder.tvQuantityMyOrder.setText(String.valueOf(products.get(position).getQuantity()));
        // 商品的价格
        mViewHolder.tvPriceMyOrder.setText(df.format(products.get(position).getPrice()));
        // 商品的税率
        mViewHolder.tvTaxRateMyOrder.setText(df.format(products.get(position).getTaxRate() * 100) + "%");
        //显示图片
        imageLoader.displayImage(products.get(position).getImage(), mViewHolder.imProductMyOrder);
        // 取消订单
        mViewHolder.btCancelMyOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.cancelOrder(products.get(position).getOrderCoderForgroup());
            }
        });
        // 立即支付
        mViewHolder.btImmediatePayMyOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OrderPayment.class);
                intent.putExtra("orderCode", products.get(position).getOrderCoderForgroup());
                context.startActivity(intent);

            }
        });
        //如果是第一个product 就显示订单号标题布局
        if (products.get(position).isFirst()) {
            mViewHolder.RelativeLayoutTitleMyOrder.setVisibility(View.VISIBLE);
        } else {
            mViewHolder.RelativeLayoutTitleMyOrder.setVisibility(View.GONE);
        }
        // 如果是最后一个布局显示结算相关的布局
        if (products.get(position).isLast()) {
            mViewHolder.PayDetailRelativeLayoutMyOrder.setVisibility(View.VISIBLE);
            mViewHolder.payDataRelativeLayout.setVisibility(View.VISIBLE);
            mViewHolder.fenge.setVisibility(View.VISIBLE);
        } else {
            mViewHolder.PayDetailRelativeLayoutMyOrder.setVisibility(View.GONE);
            mViewHolder.payDataRelativeLayout.setVisibility(View.GONE);
            mViewHolder.fenge.setVisibility(View.GONE);
        }

        // 通过当前商品的订单号找到结算详情
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getOrderCode().equals(products.get(position).getOrderCoderForgroup())
                    && (products.get(position).isLast() || products.get(position).isFirst())) {
                // 设置当前订单的状态
                String status = data.get(i).getStatusName();
                if (status.equals("订单关闭") || status.equals("已取消")) {
                    mViewHolder.tvPayStatusMyOrder.setTextColor(context.getResources().getColor(R.color.colorSomber));
                }else {
                    mViewHolder.tvPayStatusMyOrder.setTextColor(context.getResources().getColor(R.color.color_bt_balance_over50));
                }
                mViewHolder.tvPayStatusMyOrder.setText(status);
                // 商品的总价
                mViewHolder.tvTotalPriceMyOrder.setText(String.valueOf(df.format(data.get(i).getTotal())));
                //运费
                mViewHolder.tvFreightMyOrder.setText(String.valueOf(df.format(data.get(i).getFreight())));
                //订单关税
                mViewHolder.tvTaxMyOrder.setText(String.valueOf(df.format(data.get(i).getTax())));
                // 实付款
                mViewHolder.tvRealPaymentMyOrder.setText(String.valueOf(df.format(data.get(i).getTotal())));
                //下单时间
                Date dat = new Date(data.get(i).getDateAdded());
                GregorianCalendar gc = new GregorianCalendar();
                gc.setTime(dat);
                java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
                String sb = format.format(gc.getTime());

                mViewHolder.tvDateAddedMyOrder.setText(sb);
                //待付款
                if ("waiting_for_payment".equals(data.get(i).getStatusCode().toString())) {
                    mViewHolder.immediatelyPayLinearLayoutMyOrder.setVisibility(View.VISIBLE);
                } else {
                    mViewHolder.immediatelyPayLinearLayoutMyOrder.setVisibility(View.GONE);
                }
                // 待收货
                if ("waiting_for_delivery".equals(data.get(i).getStatusCode().toString())
                        || "delivered".equals(data.get(i).getStatusCode().toString())
                        || "waiting_for_clearance".equals(data.get(i).getStatusCode().toString())) {
                    // 在最后一件商品显示相关布局
                    if (products.get(position).isLast()) {
                        mViewHolder.confirmReceivingLinearLayoutMyOrder.setVisibility(View.VISIBLE);
                    }

                } else {
                    mViewHolder.confirmReceivingLinearLayoutMyOrder.setVisibility(View.GONE);
                }
                // 交易完成
                if ("done".equals(data.get(i).getStatusCode().toString())) {
                    // 评论按钮相关的布局，在最后一件商品显示相关布局
                    if (products.get(position).isLast()) {
                        mViewHolder.ReviewProductLinearLayoutMyOrder.setVisibility(View.VISIBLE);
                    } else {
                        mViewHolder.ReviewProductLinearLayoutMyOrder.setVisibility(View.GONE);
                    }
                } else {
                    mViewHolder.ReviewProductLinearLayoutMyOrder.setVisibility(View.GONE);
                }

            }
        }
        // 查看物流
        mViewHolder.btLogistics_Confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, LogitsticsCheck.class);
                intent.putExtra("orderCode", products.get(position).getOrderCoderForgroup());
                context.startActivity(intent);

            }
        });
        mViewHolder.btLogistics_Review.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LogitsticsCheck.class);
                intent.putExtra("orderCode", products.get(position).getOrderCoderForgroup());
                context.startActivity(intent);
            }
        });

        //评价商品
        mViewHolder.btReviewGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ReviewActivity.class);
                intent.putExtra("orderCode", products.get(position).getOrderCoderForgroup());
                context.startActivityForResult(intent, 100);
            }
        });
        // 确认收货
        mViewHolder.btReceiptConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int cc = -1;
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).getOrderCode().equals(products.get(position).getOrderCoderForgroup())) {
                        cc = i;
                        break;
                    }
                }
                if (cc != -1 && "等待商家发货".equals(data.get(cc).getStatusName().toString())) {
                    Toast.makeText(context, "商家还未发货！", Toast.LENGTH_LONG).show();
                    return;
                } else if (cc == -1) {
                    return;
                }
                context.confirmRecipt(products.get(position).getOrderCoderForgroup());
            }
        });
        return convertView;
    }

    class ViewHolder {
        // 商品的名称
        TextView tvProductNameMyOrder;
        // 当前商品的数量
        TextView tvQuantityMyOrder;
        // 商品的价格
        TextView tvPriceMyOrder;
        // 税率
        TextView tvTaxRateMyOrder;
        // 付款提示布局
        RelativeLayout payDataRelativeLayout;
        // 付款详情布局
        RelativeLayout PayDetailRelativeLayoutMyOrder;
        // 订单号
        TextView tvOrderCodeMyOrder;
        // 订单号显示标题布局
        RelativeLayout RelativeLayoutTitleMyOrder;
        // 当前订单的状态
        TextView tvPayStatusMyOrder, fenge;
        //商品的总价
        TextView tvTotalPriceMyOrder;
        //运费
        TextView tvFreightMyOrder;
        //订单关税
        TextView tvTaxMyOrder;
        // 实付款
        TextView tvRealPaymentMyOrder;
        // 下单时间
        TextView tvDateAddedMyOrder;
        // 付款时限
        TextView tvDateModified;
        // 立即支付布局
        RelativeLayout immediatelyPayLinearLayoutMyOrder;
        //确认收货布局
        RelativeLayout confirmReceivingLinearLayoutMyOrder;
        //商品评价布局
        RelativeLayout ReviewProductLinearLayoutMyOrder;
        //商品的图片
        ImageView imProductMyOrder;
        //item线性布局
        LinearLayout LLinerLayoutMyOrder;
        //取消订单
        Button btCancelMyOrder;
        //立即支付
        Button btImmediatePayMyOrder;
        //查看物流
        Button btLogistics_Confirm;
        Button btLogistics_Review;
        // 评价商品
        Button btReviewGoods;
        //确认收货
        Button btReceiptConfirm;
    }
}